<script lang="ts" setup>
import FormItem from "@/components/autoForm/FormItem.ts";
import { reactive, ref } from "vue";
import { LockOutlined, UserOutlined } from "@ant-design/icons-vue";
import { codeImage, loginPost, LoginResponse } from "@/api/user.ts";
import { formItems, IFormState, ResPage } from "@/views/login/data.ts";
import { useRouter } from "vue-router";
import config from "@/utils/config.ts";

const router = useRouter();
const formState: IFormState = reactive({
  username: "",
  password: "",
  code: "",
  uuid: "",
});
const codeImageSrc = ref("");
const changeCodeHandle = () => {
  codeImage().then((res: ResPage<never>) => {
    codeImageSrc.value = `data:image/gif;base64,${res.img}`;
    formState.uuid = res.uuid || "";
  });
};
changeCodeHandle();
const handleFinish = () => {
  loginPost(formState).then((data: ResPage<LoginResponse>) => {
    if (data && data.token) {
      localStorage.setItem("token", data.token);
      const path = "/home";
      router.push({ path });
    } else {
      changeCodeHandle();
    }
  });
};
</script>

<template>
  <div class="login-wrapper">
    <div class="login-header">
      <div class="header-logo">
        <p>{{ config.title }}</p>
      </div>
    </div>
    <div class="deep-img">
      <img alt="" src="../../assets/login_bg.png" />
    </div>
    <div class="login-content">
      <h2>{{ config.subTitle }}</h2>
      <div class="form-wrapper">
        <img alt="" class="login-img" src="../../assets/login_img.png" />
        <a-form
          :model="formState"
          class="login-form"
          layout="vertical"
          @finish="handleFinish"
        >
          <template v-for="item in formItems" :key="item">
            <a-form-item :name="item.field">
              <FormItem v-model:value="formState[item.field]" :option="item">
                <template #user>
                  <UserOutlined style="color: rgb(0 0 0 / 25%)" />
                </template>
                <template #lock>
                  <LockOutlined style="color: rgb(0 0 0 / 25%)" />
                </template>
                <template #code>
                  <img
                    :src="codeImageSrc"
                    :width="80"
                    alt=""
                    @click="changeCodeHandle"
                  />
                </template>
              </FormItem>
            </a-form-item>
          </template>
          <a-form-item>
            <a-button
              :disabled="
                formState.username === '' ||
                formState.password === '' ||
                formState.code === ''
              "
              block
              html-type="submit"
              type="primary"
              size="large"
            >
              登 录
            </a-button>
          </a-form-item>
        </a-form>
      </div>
    </div>
    <div class="login-bottom">陕ICP备18007192号</div>
  </div>
</template>

<style lang="less" scoped>
.login-wrapper {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  overflow: hidden;

  .login-header {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    height: 60px;
    padding: 10px 20px;

    .header-logo {
      padding-left: 40px;
      background: url("../../assets/top_logo.png") no-repeat;
      background-size: contain;
    }
  }

  .deep-img {
    position: absolute;
    top: 42px;
    right: 0;
    left: 0;
  }

  .login-content {
    z-index: 1;
    width: 900px;
    padding: 20px 30px;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0 2px 10px rgb(153 153 153 / 30%);

    .form-wrapper {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 75px;

      .login-img {
        width: 300px;
        height: 300px;
      }

      .login-form {
        position: relative;

        &::before {
          position: absolute;
          top: -50px;
          left: -100px;
          width: 1px;
          height: 325px;
          background: #e8e8e8;
          content: "";
        }
      }
    }
  }

  .login-bottom {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    font-size: 14px;
    line-height: 34px;
    color: rgb(0 0 0 / 65%);
    text-align: center;
  }
}
</style>
